package com.ssy.lingxi.system.service.impl;

import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.PageModel;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.member.merchant.api.feign.paas.BaseLevelRightCreditControllerFeign;
import com.ssy.lingxi.member.merchant.api.model.vo.paas.MemberCreditConfigVO;
import com.ssy.lingxi.system.dao.CreditIndexRepository;
import com.ssy.lingxi.system.dto.request.CreditIndexRequest;
import com.ssy.lingxi.system.dto.request.CreditIndexUpdateRequest;
import com.ssy.lingxi.system.entity.CreditIndex;
import com.ssy.lingxi.system.service.CreditIndexService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* 信用指数评估规则配置服务层
* @author wrc
* @date 2020/6/5
*/
@Service
public class CreditIndexServiceImpl  implements CreditIndexService {

    @Resource
    CreditIndexRepository creditIndexRepository;

    @Resource
    BaseLevelRightCreditControllerFeign baseLevelRightCreditControllerFeign;

    /**
     * 信用指数评估规则配置服务层 列表查询
     * @author wrc
     * @date 2020/6/5
     */
    @Override
    public Wrapper<PageData<CreditIndex>> creditIndexList(PageModel reqquest) {
        Page<CreditIndex> all = creditIndexRepository.findAll(PageRequest.of(reqquest.getCurrent(), reqquest.getPageSize()));
        return Wrapper.success(new PageData<>(all.getTotalElements(),all.getContent()));
    }

    /**
     * 停用或者启用信用
     * @param request
     * @author wrc
     * @date 2020/6/5
     */
    @Override
    public Wrapper creditIndexStopAndStart(CreditIndexRequest request) {
        CreditIndex creditIndex = creditIndexRepository.findById(request.getId()).orElse(null);
        if(creditIndex == null){
            return Wrapper.failService("信用指数评估规则不存在请检查参数");
        }

        MemberCreditConfigVO memberCreditConfigVO=new MemberCreditConfigVO();
        memberCreditConfigVO.setCreditTypeEnum(creditIndex.getTypeEnum());
        memberCreditConfigVO.setCreditPoint(0);
        memberCreditConfigVO.setStatus(request.getState());
        Wrapper<?> wrapper = baseLevelRightCreditControllerFeign.updateMemberCreditConfigStatus(memberCreditConfigVO);
        if(wrapper.getCode() != ResponseCode.SUCCESS.getCode()) {
            return Wrapper.fail(wrapper.getCode(), wrapper.getMessage());
        }

        creditIndex.setUpdateTime(System.currentTimeMillis());
        creditIndex.setState(request.getState());
        creditIndexRepository.save(creditIndex);

        return Wrapper.success();
    }

    /**
     * 修改信用权重配置
     * @param request
     * @author wrc
     * @date 2020/6/5
     */
    @Override
    public Wrapper creditIndexUpdate(CreditIndexUpdateRequest request) {
        CreditIndex creditIndex = creditIndexRepository.findById(request.getId()).orElse(null);
        if(creditIndex == null){
            return Wrapper.failService("信用指数评估规则不存在请检查参数");
        }

        MemberCreditConfigVO memberCreditConfigVO=new MemberCreditConfigVO();
        memberCreditConfigVO.setCreditTypeEnum(creditIndex.getTypeEnum());
        memberCreditConfigVO.setCreditPoint(request.getWeight());
        memberCreditConfigVO.setStatus(1);
        Wrapper<?> wrapper = baseLevelRightCreditControllerFeign.updateMemberCreditConfigPoint(memberCreditConfigVO);
        if(wrapper.getCode() != ResponseCode.SUCCESS.getCode()) {
            return Wrapper.fail(wrapper.getCode(), wrapper.getMessage());
        }

        creditIndex.setWeight(request.getWeight());
        creditIndex.setUpdateTime(System.currentTimeMillis());
        creditIndexRepository.save(creditIndex);

        return Wrapper.success();
    }
}
